﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using DevComponents.DotNetBar;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// Style下拉颜色控件
    /// </summary>
    public partial class DropDownStyleColor : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DropDownStyleColor()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            btnShape.Click += new EventHandler(btnShape_Click);
            btnDown.Click += new EventHandler(ShowColor);
            //InitStyle();
        }

        /// <summary>
        /// 获取或设置颜色选择类型的图标
        /// </summary>
        public Image ImageShape
        {
            get { return btnShape.Image; }
            set { btnShape.Image = value; }
        }

        /// <summary>
        /// 获取或设置下拉的图标
        /// </summary>
        public Image ImageDown
        {
            get { return btnDown.Image; }
            set { btnDown.Image = value; }
        }

        /// <summary>
        /// 是否显示图标
        /// </summary>
        public bool ImageVisiable
        {
            get { return btnShape.Visible; }
            set { btnShape.Visible = value; }
        }

        /// <summary>
        /// 提示消息
        /// </summary>
        public string ToolTip
        {
            get { return btnShape.Tooltip; }
            set
            {
                btnShape.Tooltip = value;
                btnDown.Tooltip = value;
            }
        }

        /// <summary>
        /// 当前选择的颜色
        /// </summary>
        private Color _SelectedColor = Color.White;

        /// <summary>
        /// 获取或设置当前选择的颜色
        /// </summary>
        [Browsable(false)]
        public Color SelectedColor
        {
            get { return _SelectedColor; }
            set
            {
                if (_SelectedColor.Equals(value)) return;
                _SelectedColor = value;
                pnlColor.BackColor = _SelectedColor;
            }
        }

        /// <summary>
        /// 颜色按钮容器
        /// </summary>
        private Form FrmColor = null;

        /// <summary>
        /// 当前颜色选择改变事件
        /// </summary>
        public event ColorEventHandler SelectedChanged = null;

        /// <summary>
        /// 初始化Style颜色到颜色容器控件中
        /// </summary>
        public void InitStyle()
        {
            string f = string.Format("{0}\\{1}", AoHelper.StylePath, "ESRI.style");
            if (!File.Exists(f)) return;
            List<SymbolItem> styles = SymbolHelper.GetStyles(zjgisStyleGalleryType.Colors, f);
            if (styles == null || styles.Count == 0) return;

            FrmColor = new Form();
            FrmColor.FormBorderStyle = 0;
            FrmColor.StartPosition = 0;
            FrmColor.ShowInTaskbar = false;
            FrmColor.BackColor = (SystemColors.Control);
            FrmColor.Activated += new EventHandler(FrmColor_Activated);
            FrmColor.Deactivate += new EventHandler(FrmColor_Deactivate);

            FrmColor.SuspendLayout();
            int index = 0;
            Color nocolor = Color.FromName("ffedeada");
            ButtonX btnNoColor = new ButtonX();
            btnNoColor.Text = "没有颜色";
            btnNoColor.Location = new System.Drawing.Point(4, 4);
            btnNoColor.Size = new Size(236, 20);
            btnNoColor.Tooltip = "没有颜色";
            btnNoColor.Tag = nocolor;
            btnNoColor.Name = GetName(nocolor);
            btnNoColor.Click += new EventHandler(btnColor_Click);
            FrmColor.Controls.Add(btnNoColor);
            int colorCount = 0;
            foreach (SymbolItem itm in styles)
            {
                IColor color1 = (IColor)itm.Symbol;
                colorCount++;
                if ((colorCount % 13) == 0)
                {
                    colorCount = 1;
                    index++;
                }
                ButtonX btnColor = new ButtonX();
                btnColor.ColorTable = eButtonColor.Flat;
                btnColor.Text = "";
                btnColor.Size = new Size(16, 16);
                Color tmpcolor = ColorTranslator.FromOle(color1.RGB);
                btnColor.BackColor = tmpcolor;
                btnColor.Name = GetName(tmpcolor);
                btnColor.Location = new System.Drawing.Point((4 * colorCount) + ((colorCount - 1) * 16), ((4 * (index + 2)) + (16 * index)) + 20);
                btnColor.Tooltip = itm.Name;
                btnColor.Click += new EventHandler(btnColor_Click);
                FrmColor.Controls.Add(btnColor);
            }

            ButtonX btnMore = new ButtonX();
            btnMore.Text = ("更多颜色");
            btnMore.Location = new System.Drawing.Point(4, ((4 * (index + 3)) + (16 * (index + 1))) + 20);
            btnMore.Size = new Size(236, 20);
            btnMore.Click += new EventHandler(btnMore_Click);
            FrmColor.Controls.Add(btnMore);
            FrmColor.Size = new Size(btnMore.Width + 8, btnMore.Bottom + 4);
            FrmColor.ResumeLayout(false);
        }

        /// <summary>
        /// 显示颜色控件容器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowColor(object sender, EventArgs e)
        {
            if (FrmColor == null) return;
            Rectangle rectangle1 = this.RectangleToScreen(this.ClientRectangle);
            if ((rectangle1.Y + FrmColor.Height) > Screen.PrimaryScreen.WorkingArea.Height)
            {
                if ((rectangle1.X + FrmColor.Width) > Screen.PrimaryScreen.WorkingArea.Width)
                    FrmColor.Location = new System.Drawing.Point(rectangle1.X - FrmColor.Width + this.Width, rectangle1.Y - FrmColor.Height);
                else
                    FrmColor.Location = new System.Drawing.Point(rectangle1.X, rectangle1.Y - FrmColor.Height);
            }
            else
            {
                if ((rectangle1.X + FrmColor.Width) > Screen.PrimaryScreen.WorkingArea.Width)
                    FrmColor.Location = new System.Drawing.Point(rectangle1.X - FrmColor.Width + this.Width, rectangle1.Y + this.Height);
                else
                    FrmColor.Location = new System.Drawing.Point(rectangle1.X, rectangle1.Y + this.Height);
            }
            string name = GetName(SelectedColor);
            Control[] ctls = FrmColor.Controls.Find(name, true);
            if (ctls != null && ctls.Length > 0)
            {
                ctls[0].Focus();
            }
            //FrmColor.Show(this.ParentForm);
            FrmColor.Visible = true;
            FrmColor.BringToFront();
            FrmColor.Activate();

        }

        /// <summary>
        /// 获取颜色的名称
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private string GetName(Color color)
        {
            return "C" + color.R.ToString() + color.G.ToString() + color.B.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnShape_Click(object sender, EventArgs e)
        {
            OnSelectColor();
        }

        /// <summary>
        /// 选择更多的颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnMore_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            dlg.AllowFullOpen = true;
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                SelectedColor = dlg.Color;
                FrmColor.Visible = false;
                SelectedChanged?.Invoke(this, SelectedColor);
                OnSelectColor();
            }
        }

        /// <summary>
        /// 选择颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnColor_Click(object sender, EventArgs e)
        {
            ButtonX btn = (ButtonX)sender;
            if (btn.Tag == null)
                SelectedColor = btn.BackColor;
            else
                SelectedColor = (Color)btn.Tag;
            FrmColor.Visible = false;
            SelectedChanged?.Invoke(this, SelectedColor);
            OnSelectColor();
        }

        /// <summary>
        /// 颜色窗体容器失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FrmColor_Deactivate(object sender, EventArgs e)
        {
            FrmColor.Visible = false;
        }

        /// <summary>
        /// 颜色窗体容器获得焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FrmColor_Activated(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual void OnSelectColor()
        {

        }
    }

    /// <summary>
    /// 颜色事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="color"></param>
    public delegate void ColorEventHandler(object sender,Color color);
    
}
